package com.kitty.game.task.service;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.model.EquipBox;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.service.message.*;
import com.kitty.game.task.service.taskHandler.DigTreasureTaskHandler;
import com.kitty.game.utils.Const;
import com.kitty.mina.message.MessagePusher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service
public class CangBaoTuService {
    @Autowired
    EquipService equipService;

    /**
     * {
     *    "pos": 42,
     *    "amount": 1,
     *    "name": "auto"
     * }
     * @param role
     * @param allAutoCangBaoTu
     */
    public void autoCangBaoTu(Role role, AllAutoCangBaoTu allAutoCangBaoTu){
        /**需要先验证账号权限*/
        String type = allAutoCangBaoTu.getName();
        //表示自动任务
        if("auto".equals(type)){
            int pos = allAutoCangBaoTu.getPos();
            int newPos = pos & 0x0FF;
            RoleEquip roleEquip = equipService.getRoleEquipByPos(role, newPos);
            String itemName = roleEquip.getName();
            /**防止玩家使用wpe发包*/
            if (roleEquip.queryNum() < 1) {
                //关闭自动挖宝
                RespCloseAutoCangBaoTu respCloseAutoCangBaoTu  = new RespCloseAutoCangBaoTu();
                respCloseAutoCangBaoTu.setItemName(itemName);
                MessagePusher.pushMessage(role, respCloseAutoCangBaoTu);
                return;
            }
            if (Const.TREASURE_SUPER_ITEM_NAME.equals(itemName) || Const.TREASURE_SUPER_SENIOR_ITEM_NAME.equals(itemName)) {
                RespOpenAutoCangBaoTu respOpenAutoCangBaoTu = new RespOpenAutoCangBaoTu();
                respOpenAutoCangBaoTu.setItemName(itemName);
                MessagePusher.pushMessage(role, respOpenAutoCangBaoTu);
                handleAutoWaBao(role,itemName,roleEquip);
            }
        }
    }
    public void autoCangBaoTu(Role role, ReqOpenAutoCangBaoTu reqOpenAutoCangBaoTu){
        String itemName = reqOpenAutoCangBaoTu.getItemName();
        EquipBox equipBox = role.getEquipBox();
        Set<RoleEquip> equips = new HashSet<>(equipBox.getEquips().values());
        for (RoleEquip roleEquip : equips) {
            if (Const.TREASURE_SUPER_ITEM_NAME.equals(itemName) || Const.TREASURE_SUPER_SENIOR_ITEM_NAME.equals(itemName)) {
                if(roleEquip.getName().equals(itemName) || roleEquip.getName().equals(itemName) ){
                    RespOpenAutoCangBaoTu respOpenAutoCangBaoTu = new RespOpenAutoCangBaoTu();
                    respOpenAutoCangBaoTu.setItemName(itemName);
                    MessagePusher.pushMessage(role, respOpenAutoCangBaoTu);
                    handleAutoWaBao(role,itemName,roleEquip);
                    return;
                }
            }
        }
        //如果没有找到对应的藏宝图，说明已经没有了。
        //关闭自动挖宝
        RespCloseAutoCangBaoTu respCloseAutoCangBaoTu  = new RespCloseAutoCangBaoTu();
        respCloseAutoCangBaoTu.setItemName(itemName);
        MessagePusher.pushMessage(role, respCloseAutoCangBaoTu);

    }
    //关闭自动挖宝
    public void stopAuto(Role role, ReqCloseAutoCangBaoTu reqOpenAutoCangBaoTu){
        RespCloseAutoCangBaoTu respCloseAutoCangBaoTu  = new RespCloseAutoCangBaoTu();
        respCloseAutoCangBaoTu.setItemName(reqOpenAutoCangBaoTu.getItemName());
        MessagePusher.pushMessage(role, respCloseAutoCangBaoTu);
    }

    //自动任务开启
    public void handleAutoWaBao(Role role,String itemName,RoleEquip roleEquip) {
        role.pushTempCache("auto_wabao", 1);
        if (itemName.equals(Const.TREASURE_NORMAL_ITEM_NAME)) {
            // 产生寻宝任务
            SpringUtils.getBean(DigTreasureTaskHandler.class).useTreasureMapItem(role, Const.DIG_TREASURE_TYPE_NORMAL, roleEquip);
            return;
        }
        if (itemName.equals(Const.TREASURE_SUPER_ITEM_NAME)) {
            // 产生寻宝任务
            SpringUtils.getBean(DigTreasureTaskHandler.class).useTreasureMapItem(role, Const.DIG_TREASURE_TYPE_SUPER, roleEquip);
            return;
        }
        if (itemName.equals(Const.TREASURE_SUPER_SENIOR_ITEM_NAME)) {
            // 产生寻宝任务
            SpringUtils.getBean(DigTreasureTaskHandler.class).useTreasureMapItem(role, Const.DIG_TREASURE_TYPE_SUPER_SENIOR, roleEquip);
            return;
        }
    }
    //发送开启自动任务消息给客户端

}
